package com.neusoft.crm.service.impl;

import com.neusoft.crm.mapper.SysUserMapper;
import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.ISysUserService;
import com.neusoft.crm.utils.Slf4j2Logger;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统用户服务实现类
 *
 * @author msprc
 */
@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Cacheable(value = "locateSysUser", key = "'sysUser:'+#sysUser.userName +'passWord'+ #sysUser.userPassword", unless = "#result==null")
    @Override
    public SysUser selectSysUserByNameByPass(SysUser sysUser) {
        return sysUserMapper.selectSysUserByNameByPass(sysUser);
    }

    @Cacheable(value = "locateSysUser", key = "'List: sysUser: locateSysUserList'", unless = "#result==null")
    @Override
    public List<SysUser> selectSystemUserList(SysUser sysUser) {
        return sysUserMapper.selectSystemUserList(sysUser);
    }


    @CacheEvict(value = "locateSysUser", allEntries = true)
    @Override
    public int addSystemUserInfo(SysUser sysUser) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = sysUserMapper.addSystemUserInfo(sysUser);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("系统用户创建失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @CacheEvict(value = "locateSysUser", allEntries = true)
    @Override
    public int updateSystemUserInfo(SysUser sysUser) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = sysUserMapper.updateSystemUserInfo(sysUser);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("系统用户编辑失败，失败原因为：", e);
        }
        return influenceLineCount;
    }

    @CacheEvict(value = "locateSysUser", allEntries = true)
    @Override
    public int deleteSystemUser(SysUser sysUser) {
        int influenceLineCount = 0;
        try {
            influenceLineCount = sysUserMapper.deleteSystemUser(sysUser);
        } catch (PersistenceException e) {
            Slf4j2Logger.LOGGER.error("系统用户删除失败，失败原因为：", e);
        }
        return influenceLineCount;
    }


}
